//
//  WDWorderOrdRevVehDetailCarInfoView.m
//  wd-serviceapp
//
//  Created by Bin Tang on 2024/8/31.
//

#import "WDWorderOrdRevVehDetailCarInfoView.h"
#import "WDArrivalServiceDetailModel.h"

@interface WDWorderOrdRevVehDetailCarInfoView ()
@property (nonatomic, strong) WDArrivalServiceDetailModel *taskDetailModel;

/// 产品代码
@property (nonatomic, strong) WDTaskDetailBasicSingleView *productCodeView;

/// 底盘编码(vin)
@property (nonatomic, strong) WDTaskDetailBasicSingleView *vinCodeView;

/// 保修时故障码
@property (nonatomic, strong) WDTaskDetailBasicSingleView *faultCodeView;

@end

@implementation WDWorderOrdRevVehDetailCarInfoView

- (void)initWithSubView {
    [super initWithSubView];
    self.backgroundColor = [UIColor whiteColor];
    
}

/// 产品代码
- (void)initWithProductCodeView {
//    if (!_productCodeView) {
//        CGRect frame = CGRectMake(0, 0, self.width, kWDTaskDetailBasicSingleViewHeight);
//        _productCodeView = [[WDTaskDetailBasicSingleView alloc] initWithFrame:frame];
//        [self addSubview:_productCodeView];
//    }
//    if (self.taskDetailModel) {
//        [_productCodeView updateWithViewTitle:@"产品代码" value:@"" valueHeight:18];
//    }
}

/// 底盘编码
- (void)initWithVinCodeView {
    if (!_vinCodeView) {
//        CGRect frame = self.productCodeView.frame;
        CGRect frame = CGRectMake(0, 0, self.width, kWDTaskDetailBasicSingleViewHeight);
        frame.origin.y = self.productCodeView.bottom;
        _vinCodeView = [[WDTaskDetailBasicSingleView alloc] initWithFrame:frame];
        [self addSubview:_vinCodeView];
    }
    if (self.taskDetailModel) {
        [_vinCodeView updateWithViewTitle:@"底盘编码" value:self.taskDetailModel.vehicleVin valueHeight:18];
    }
}

/// 初始化电池数组
- (void)initWithBatteryList {
    WDTaskDetailBasicSingleView *preView = nil;
    CGRect frame = self.vinCodeView.frame;
    frame.origin.y = self.vinCodeView.bottom;
    
    for (int i=0; i<self.taskDetailModel.batteryList.count; i++) {
        WDBatteryInfoModel *model = self.taskDetailModel.batteryList[i];
        if (preView) {
            frame.origin.y = preView.bottom;
        }
        WDTaskDetailBasicSingleView *batteryTypeView = [[WDTaskDetailBasicSingleView alloc] initWithFrame:frame];
        [self addSubview:batteryTypeView];
        [batteryTypeView updateWithViewTitle:@"电芯种类" value:model.chipTypeValue valueHeight:18];
        frame.origin.y = batteryTypeView.bottom;
        WDTaskDetailBasicSingleView *batteryModelView = [[WDTaskDetailBasicSingleView alloc] initWithFrame:frame];
        [self addSubview:batteryModelView];
        [batteryModelView updateWithViewTitle:@"电池包序列号" value:model.serialNo valueHeight:18];
        [self.viewList addObject:batteryModelView];
        preView = batteryModelView;
    }
}

/// 保修时故障码
/*- (void)initWithFaultCodeView {
    if (!_faultCodeView) {
        CGRect frame = self.vinCodeView.frame;
        frame.origin.y = self.vinCodeView.bottom;
        _faultCodeView = [[WDTaskDetailBasicSingleView alloc] initWithFrame:frame];
        [self addSubview:_faultCodeView];
    }
    if (self.taskDetailModel) {
        if (self.viewList.count > 0) {
            UIView *view = self.viewList.lastObject;
            _faultCodeView.top = view.bottom;
        } else {
            _faultCodeView.top = _vinCodeView.bottom;
        }
        [_faultCodeView updateWithViewTitle:@"保修时故障码" value:@"暂无字段" valueHeight:18];
    }
}*/

/// 历史故障
- (void)initWithCanFaultInfo {
    WDTaskDetailBasicSingleView *preView = nil;
    CGRect frame = self.vinCodeView.frame;
    frame.origin.y = self.vinCodeView.bottom;
    if (self.viewList.count > 0) {
        UIView *view = self.viewList.lastObject;
        frame.origin.y = view.bottom;
    }
    if (self.taskDetailModel.canFaultInfoList.count == 0) {
        WDTaskDetailBasicSingleView *canView = [[WDTaskDetailBasicSingleView alloc] initWithFrame:frame];
        [self addSubview:canView];
        [canView updateWithViewTitle:@"故障码" value:@"无故障码" valueHeight:18];
        canView.valueColor = [UIColor colorWithHexString:@"#19C46E"];
        return;
    }
    
    for (int i=0; i<self.taskDetailModel.canFaultInfoList.count; i++) {
        WDCanFaultInfoModel *model = self.taskDetailModel.canFaultInfoList[i];
        if (preView) {
            frame.origin.y = preView.bottom;
        }
        WDTaskDetailBasicSingleView *canView = [[WDTaskDetailBasicSingleView alloc] initWithFrame:frame];
        [self addSubview:canView];
        NSString *value = [NSString stringWithFormat:@"%@-%@-%@", model.faultName, model.faultCode, model.stimetsStr];
        if (i == 0) {
            [canView updateWithViewTitle:@"故障码" value:value valueHeight:18];
        } else {
            [canView updateWithViewTitle:@"" value:value valueHeight:18];
        }
        canView.valueColor = [UIColor colorWithHexString:@"#19C46E"];
        preView = canView;
    }
    
}

- (void)updateWithViewData:(id)data {
    self.taskDetailModel = data;
    
    /// 产品代码
//    [self initWithProductCodeView];

    /// 底盘编码
    [self initWithVinCodeView];
    
    if (self.taskDetailModel.batteryList.count > 0) {
        [self initWithBatteryList];
    }
    
    /// 保修时故障码
//    [self initWithFaultCodeView];
    [self initWithCanFaultInfo];
}

@end
